package com.news.model;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;


/**
 * connect to DAO and convert the result data to JSON
 * 
 * @author ron
 * 
 */
public class NewsJsonHandler {

	public NewsJsonHandler() {
		super();
	}

	public JSONArray toJSONArray(List<NewsVO> newsList) {
		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		for (NewsVO news : newsList) {
			HashMap map = new HashMap();
			Integer news_id = news.getNews_id();
			String title = news.getTitle();
			String content = news.getContent();
			DateFormat df = DateFormat.getDateInstance();
			String time = df.format(news.getTime());
			
			map.put("news_id", news_id);
			map.put("title", title);
			map.put("content", content);
			map.put("time", time);
			list.add(map);
		}
		JSONArray newsJson = new JSONArray(list);
		return newsJson;
	}

}
